#-*- coding: utf-8 -*-

## http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.plot - цвета и типы линий

import locale
import matplotlib.pyplot as plt

class Graph:

    def __init__(self):
	self.plt = plt
	self.lines = []
	self.legend = []
	locale.setlocale(locale.LC_NUMERIC, 'C')
	self.plt.clf()
    
    def flip(self, x):
	y = []
	for i in x:
	    y.append(-i)
	return y

    def add(self, x, y, linetype=None, legend=None, linewidth=1):
	locale.setlocale(locale.LC_NUMERIC, 'C')
	line = None
	if legend == None:
	    legend = '_nolegend_'
	if linetype != None:
	    line = self.plt.plot(x, y, linetype, linewidth=linewidth, label=legend)
	else:
	    line= self.plt.plot(x, y, linewidth=linewidth, label=legend)
	#if legend != None:
	    #self.lines.append(line)
	    #self.legend.append(legend)
	

    def axes(self, wide = False):
	xmin = wide and -1.03 or -0.03
	xmax =  1.03
	ymin = -0.03
	ymax =  1.03
	step =  0.25
	self.plt.axis([xmin, xmax, ymin, ymax])
	
	#axes
	self.plt.axhline(y=0, xmin=int(xmin), xmax=int(xmax), color='k')
	self.plt.axvline(x=0, ymin=int(ymin), ymax=int(ymax), color='k')
	
	#ticks
	ticks = []
	i = int(xmin)
	while i <= xmax:
	    ticks.append(i)
	    i += step
	self.plt.xticks(ticks)
	
	ticks = []
	i = int(ymin)
	while i <= ymax:
	    ticks.append(i)
	    i += step
	self.plt.yticks(ticks)
	
	#grid
	self.plt.grid(True)

    def build(self, wide = False, showLegend = False):
	self.plt.xlabel('x')
	self.plt.ylabel('f(x)')
	
	
	#if len(self.legend) and showLegend:
	#    self.plt.legend(self.lines, self.legend, # список легенды
	#    		loc='best')      # положение легенды #best
	
	if showLegend:
	    self.plt.legend(loc='upper center', bbox_to_anchor=(0.,1.02, 1, .102))
	
	#axes
	self.axes(wide)

    def show(self, wide=False, showLegend=False):
	self.build(wide, showLegend)
	self.plt.show()
    
    def save(self, filename, wide=False, showLegend=False):
	self.build(wide, showLegend)
	self.plt.savefig(filename, dpi=200)



